package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ProfitMXActivity extends AppCompatActivity {
    private String account;
    private RecyclerView ProfitMXtab;
    private ImageButton ProfitMXfh;
    private JSONArray jsonObject1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_profit_mx);
        init();


        final RefreshLayout refreshLayout1 = (RefreshLayout)findViewById(R.id.ProfitMXLayout);
        refreshLayout1.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        if(jsonObject1==null) {
                            Toast.makeText(ProfitMXActivity.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout1.finishRefresh();
                        }else{
                            Toast.makeText(ProfitMXActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout1.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });


        //返回按钮
        ProfitMXfh = findViewById(R.id.ProfitMXfh);
        ProfitMXfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void  init(){

        Object user_info = Preferences.getParam(ProfitMXActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());
            //账号
            account = myJson.get("account").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1016(account,"0", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                try {
                    jsonObject1 = (JSONArray) jsonObject.get("data");
                    ProfitMXtab = findViewById(R.id.ProfitMXtab);
                    ProfitMXtab.setLayoutManager(new LinearLayoutManager(ProfitMXActivity.this));
                    ProfitMXtab.setAdapter(new BillAdapter(ProfitMXActivity.this, jsonObject1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(ProfitMXActivity.this,string);
            }
        });
    }

    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
